package sjms.behavior.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Description:
 * @Author fcb
 * @Date 2020/6/9 13:10
 */
public class ConcreteSubject implements Subject {

    private String state;

    private List<Observer> list = new ArrayList<>();

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
        this.notifyAllObservers();
    }

    @Override
    public void attach(Observer observer) {
        list.add(observer);
    }

    @Override
    public void detach(int index) {
        list.remove(index);
    }

    @Override
    public void notifyAllObservers() {
        for(Observer ob : list){
            ob.update();
        }
    }
}
